package server;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.Socket;

/**
 * Connection
 * 
 * Wrapper class to represent a Client connection
 * 
 * Rep Invariant: s != null
 *
 */
public class Connection {

    private BufferedReader in;
    
    private PrintWriter out;
    
    private Socket s;
    
    /**
     * Constructor for Connection 
     * 
     * Rep Invariant: s != null
     * 
     * @param s the socket where the client is connected to
     * @throws IOException
     */
    public Connection(Socket s) throws IOException{
        assert s != null;
        this.in = new BufferedReader(new InputStreamReader(
                s.getInputStream()));
        this.out = new PrintWriter(s.getOutputStream(), true);
        this.s = s;
    }
    
    /**
     * Get the BufferedReader associated with this connection
     * 
     * @return in 
     */
    public BufferedReader getBufferedReader(){
        return in;
    }
    
    
    /**
     * Get the BufferedReader associated with this connection
     * 
     * @return out
     */
    public PrintWriter getPrintWriter(){
        return out;
    }
    
    /**
     * Close the connection
     * 
     * @throws IOException
     *             if an I/O error occurs when closing this socket.
     */
    public void close() throws IOException{
        s.close();
    }
    
    
}
